package com.lwj.mallchat.common.common.event.listener;

import com.lwj.mallchat.common.common.event.UserOnlineEvent;
import com.lwj.mallchat.common.common.event.UserRegisterEvent;
import com.lwj.mallchat.common.user.dao.UserDao;
import com.lwj.mallchat.common.user.domain.entity.User;
import com.lwj.mallchat.common.user.domain.enums.UserActiveStatusEnum;
import com.lwj.mallchat.common.user.service.IPService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Date;

/**
 * @author lwj
 * @ClassName UserOnlineEventListener
 * @date 2024/10/22 16:30
 */
@Component
public class UserOnlineEventListener {

    @Autowired
    private UserDao userDao;

    @Autowired
    private IPService ipService;

    @Async
    @EventListener(classes = UserOnlineEvent.class)
    public void saveDB(UserOnlineEvent userOnlineEvent) {
        //上线更新ip地址
        User user = userOnlineEvent.getUser();
        User update = User.builder()
                .id(user.getId())
                .activeStatus(UserActiveStatusEnum.ONLINE.getStatus())
                .lastOptTime(new Date())
                .ipInfo(user.getIpInfo())
                .build();
//        update.setId(user.getId());
//        update.setActiveStatus(UserActiveStatusEnum.ONLINE.getStatus());
//        update.setLastOptTime(new Date());
//        update.setIpInfo(user.getIpInfo());
        userDao.updateById(update);
        //用户详情解析
        ipService.refreshIpDetailAsync(user.getId());
    }
}
